﻿///////////////////////////////////////////////////////////
//  SinacorPopup.cs
//  Implementation of the Class SinacorPopup
//  Created on:      06-ago-2008 18:56:00
//  Original author: POliveira.7Comm
///////////////////////////////////////////////////////////

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows;
using System.Windows.Input;

namespace Sinacor.Infra.UI.Controls.SmartClient
{
    /// <summary>
    /// Componente que utiliza o Popup para exibir um controle customizado. Não foi possível herdá-lo de Popup
    /// porque ele não possui a propriedade Template para ser customizada.
    /// </summary>
    public class SinacorPopup : ContentControl
    {
        #region Private Properties
        private Popup _parentPopup = new Popup();
        #endregion

        #region Constructors
        public SinacorPopup()
        {
            //Inicializa o Popup
            _parentPopup.AllowsTransparency = true;
            Popup.CreateRootPopup(_parentPopup, this);

            //Configura os eventos do controle
            this.CommandBindings.Add(new CommandBinding(SinacorPopupCommands.Close, btnClose_Click));
        }

        static SinacorPopup()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(SinacorPopup), new FrameworkPropertyMetadata(typeof(SinacorPopup)));
        }
        #endregion

        #region Public Properties
        /// <summary>
        /// Propriedade que controla a posição do Popup
        /// </summary>
        public static readonly DependencyProperty PlacementProperty =
            Popup.PlacementProperty.AddOwner(typeof(SinacorPopup));

        public PlacementMode Placement
        {
            get { return (PlacementMode)GetValue(PlacementProperty); }
            set { SetValue(PlacementProperty, value); }
        }

        /// <summary>
        /// Propriedade que controla a controle a que o Popup está associado
        /// </summary>
        public static readonly DependencyProperty PlacementTargetProperty =
            Popup.PlacementTargetProperty.AddOwner(typeof(SinacorPopup));

        public UIElement PlacementTarget
        {
            get { return (UIElement)GetValue(PlacementTargetProperty); }
            set { SetValue(PlacementTargetProperty, value); }
        }

        /// <summary>
        /// Propriedade que indica o retângulo onde o Popup será aberto
        /// </summary>
        public static readonly DependencyProperty PlacementRectangleProperty =
            Popup.PlacementRectangleProperty.AddOwner(typeof(SinacorPopup));

        public Rect PlacementRectangle
        {
            get { return (Rect)GetValue(PlacementRectangleProperty); }
            set { SetValue(PlacementRectangleProperty, value); }
        }

        /// <summary>
        /// Propriedade que controla o Offset horizontal do Popup
        /// </summary>
        public static readonly DependencyProperty HorizontalOffsetProperty =
            Popup.HorizontalOffsetProperty.AddOwner(typeof(SinacorPopup));

        public double HorizontalOffset
        {
            get { return (double)GetValue(HorizontalOffsetProperty); }
            set { SetValue(HorizontalOffsetProperty, value); }
        }

        /// <summary>
        /// Propriedade que controla o Offset vertical do Popup
        /// </summary>
        public static readonly DependencyProperty VerticalOffsetProperty =
            Popup.VerticalOffsetProperty.AddOwner(typeof(SinacorPopup));

        public double VerticalOffset
        {
            get { return (double)GetValue(VerticalOffsetProperty); }
            set { SetValue(VerticalOffsetProperty, value); }
        }

        /// <summary>
        /// Propriedade que indica se o Popup ficará aberto se ocorrer clique fora dele
        /// </summary>
        public static readonly DependencyProperty StaysOpenProperty =
            Popup.StaysOpenProperty.AddOwner(typeof(SinacorPopup),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, new PropertyChangedCallback(OnStaysOpenChanged)));

        public bool StaysOpen
        {
            get { return (bool)GetValue(StaysOpenProperty); }
            set { SetValue(StaysOpenProperty, value); }
        }

        /// <summary>
        /// Propriedade que indica se o Popup está aberto ou não
        /// </summary>
        public static readonly DependencyProperty IsOpenProperty =
            Popup.IsOpenProperty.AddOwner(typeof(SinacorPopup),
            new FrameworkPropertyMetadata(false));

        public bool IsOpen
        {
            get { return (bool)GetValue(IsOpenProperty); }
            set { SetValue(IsOpenProperty, value); }
        }

        /// <summary>
        /// Propriedade que contém o título do Popup
        /// </summary>
        public static readonly DependencyProperty HeaderProperty =
            DependencyProperty.Register("Header", typeof(string), typeof(SinacorPopup), new FrameworkPropertyMetadata(null));

        public string Header
        {
            get { return (string)GetValue(HeaderProperty); }
            set { SetValue(HeaderProperty, value); }
        }
        #endregion

        #region Static Methods
        private static void OnStaysOpenChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            SinacorPopup ctrl = (SinacorPopup)d;
            if ((bool)e.NewValue)
            {
                ctrl.StaysOpenParentPopup((bool)e.NewValue);
            }
        }

        private void StaysOpenParentPopup(bool newValue)
        {
            _parentPopup.StaysOpen = newValue;
        }
        #endregion

        #region Public Methods
        protected void btnClose_Click(object sender, EventArgs e)
        {
            this.IsOpen = false;
        }
        #endregion
    }
}
